//
//  NvGraphicBtn.m
//  SDKDemo
//
//  Created by ms20180425 on 2018/11/13.
//  Copyright © 2018年 meishe. All rights reserved.
//

#import "NvGraphicBtn.h"
#import "NvHeader.h"

@interface NvGraphicBtn ()

@property (nonatomic, strong) UILabel *btnLabel;

@property (nonatomic, strong) UIImageView *btnImageView;

@property (nonatomic, strong) NSString *normalString;

@property (nonatomic, strong) NSString *selectedString;

@end

@implementation NvGraphicBtn

+ (instancetype)buttonWithType:(UIButtonType)buttonType withTitle:(NSString *)title withImageNormal:(NSString *)normal withImageSelected:(NSString *)selected{
    NvGraphicBtn *btn = [NvGraphicBtn buttonWithType:buttonType];
    if (btn) {
        btn.normalString = normal;
        btn.btnImageView = [[UIImageView alloc]init];
        btn.btnImageView.contentMode = UIViewContentModeScaleAspectFit;
        if (selected) {
            btn.selectedString = selected;
            if (btn.isSelected) {
                btn.btnImageView.image = [NvUtils imageWithName:selected];
            }else{
                btn.btnImageView.image = [NvUtils imageWithName:normal];
            }
        }else{
            btn.btnImageView.image = [NvUtils imageWithName:normal];
        }
        [btn addSubview:btn.btnImageView];
        [btn.btnImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(btn);
            make.centerX.equalTo(btn);
            make.width.offset(33 * SCREANSCALE);
            make.height.offset(33 * SCREANSCALE);
        }];
        
        btn.btnLabel = [[UILabel alloc]init];
        btn.btnLabel.text = title;
        btn.btnLabel.textAlignment = NSTextAlignmentCenter;
        btn.btnLabel.font = [NvUtils regularFontWithSize:12];
        btn.btnLabel.textColor = UIColor.whiteColor;
        [btn addSubview:btn.btnLabel];
        [btn.btnLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(btn.btnImageView.mas_bottom);
            make.centerX.equalTo(btn);
            make.bottom.equalTo(btn);
        }];
        
    }
    return btn;
}

- (void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    if (self.selectedString) {
        if (selected) {
            self.btnImageView.image = [NvUtils imageWithName:self.selectedString];
        }else{
            self.btnImageView.image = [NvUtils imageWithName:self.normalString];
        }
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
